/**
 * 调用上传文件对话框
 */
Ccay.Module.define("Ccay.Upload.DialogLink",function(page,$S){
		var uploadParameters;
		var uploaderComponent ;
	
		page.init=function(uploadParam){
			uploadParameters = uploadParam;
			if(!uploadParameters.initCount){
				uploadParameters.initCount = 0;
			}
			
			uploadParameters.uploadedCount = 0 ;
	
			//如果需要批次号，并且批次号尚未生成
			if(uploadParam.needBatchId && !uploadParam.batchId){
				uploadParam.query = uploadParam.query + "&needBatchId=1";
			}
			//Ccay.Core.log(uploadParam); 
			Ccay.doGet("services/ccay/upload/support/{0}".formatURI(uploadParam.uploadType),uploadParam.query,function(uploadSetting){			
				Ccay.Core.log("batch id got:" +uploadSetting.batchId );
				//Ccay.Core.log("batch id in param :" +uploadParam.batchId );
				
				if(!uploadParam.batchId && uploadSetting.batchId){
					//Ccay.Core.log("init batch id for this upload:" +uploadSetting.batchId) ;
					uploadParam.batchId = uploadSetting.batchId;
				}
				
				uploadParameters.maxFileCount = uploadSetting.maxFileCount;
				initComponent(uploadParam,uploadSetting);
			},function(textStatus, errorThrown){
				$S("#uploader").html($.i18n.prop("ccay.common.message.unknownError").encode());
			});
	};
	
	function initComponent(uploadParam,uploadSetting){
		var uploadUrl="servlet/upload?ulType={0}&batchId={1}&".formatURI(uploadParam.uploadType, uploadParam.batchId) + uploadParam.query;
		//区分子应用
		if(uploadParam.currentContext){
			uploadUrl = "{0}/{1}".formatValue(uploadParam.currentContext,uploadUrl);
		}
		
		var uploader = new plupload.Uploader({
			runtimes : 'html5,flash,silverlight,html4,gears,browserplus',
			browse_button : uploadParam.dialogId, // you can pass in id...
			url : uploadUrl,
			max_file_size : uploadSetting.maxFileSize + 'b',
			unique_names : true,			
			max_file_count: uploadSetting.maxFileCount,
			multi_selection:(uploadSetting.maxFileCount>1),
			filters : convert(uploadSetting.fileTypesAllowed),
			init: {
				PostInit: function() {
					$("#uploadfiles").bind("click.upload",function() {
						uploader.start();
						return false;
					}) ;
				},
				FilesAdded:fileAdded,
				UploadComplete:uploadComplete,
				UploadProgress: uploadProgress,
				Error: errorFile
			}
		});
		uploader.init();
	}
	
	/**
	 * 上传文件错误方法
	 */
	function errorFile(up, err){
		Ccay.Core.log("files upload error:" +err.message);
	}
	
	/**
	 * 上传文件调用方法，进度条方法
	 */
	function uploadProgress(up, file){
		document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
	}
	
	/**
	 * 上传文件方法
	 */
	function uploadComplete(up,files){
		if(up.files.length ==0){
			$("#uploadfiles").css("overflow","hidden");
			return ;
		}
					
		if(uploadParameters.completeCallback){
			uploadParameters.completeCallback(files, uploadParameters.batchId, uploadParameters.userObject);
		}
	}
	
	/**
	 * 添加文件方法
	 */
	function fileAdded(up,files){
		plupload.each(files, function(file) {
			document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
		});
	}
	
	/**
	 *	指定上传文件的格式方法
	 */
	function convert(fileTypes){
		var p = navigator.platform;
		if(p && p.indexOf("Linux")==0){
			//var uploadType="doc,docx,xls,xlsx,xlsm,rar,zip";
			var types = fileTypes.join(",")
			return [{title : types, extensions : "*"}];
		}else{
			var types = fileTypes.join(",")
			return [{title : types, extensions : types}];
		}
	};
	
});